//
//  XGPhotoManager.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/13.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "XGPhotoManager.h"
#import "UIViewController+HXExtension.h"
#import "HXPhotoPicker.h"


@interface XGPhotoManager()<HXAlbumListViewControllerDelegate, UIAlertViewDelegate>


@property (strong, nonatomic) HXPhotoManager *singleManager;
@property (strong, nonatomic) HXDatePhotoToolManager *toolManager;
@property (strong, nonatomic) HXPhotoManager *moreManager;

@property (nonatomic, strong) NSMutableArray * listAll;


@property (nonatomic, copy) void(^image)(UIImage * image);

@end

@implementation XGPhotoManager

- (instancetype) init {
    if (self = [super init]) {
    }
    return self;
}

- (HXPhotoManager *)singleManager {
    if (!_singleManager) {
        _singleManager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _singleManager.configuration.singleSelected = YES;
        _singleManager.configuration.singleJumpEdit = YES;
        _singleManager.configuration.movableCropBox = YES;
        _singleManager.configuration.movableCropBoxEditSize = YES;
    }
    return _singleManager;
}

- (HXDatePhotoToolManager *)toolManager {
    if (!_toolManager) {
        _toolManager = [[HXDatePhotoToolManager alloc] init];
    }
    return _toolManager;
}

- (HXPhotoManager *)moreManager {
    if (!_moreManager) {
        _moreManager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _moreManager.configuration.openCamera = YES;
        _moreManager.configuration.saveSystemAblum = NO;
        _moreManager.configuration.themeColor = [UIColor blackColor];
    }
    return _moreManager;
}

- (void) openCamereWithController:(UIViewController *) controller withImage:(void(^)(UIImage * image))cameraImage {
    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        return;
    }
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"无法使用相机" message:@"请在设置-隐私-相机中允许访问相机" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
        [alert show];
        return;
    }
    [controller hx_presentCustomCameraViewControllerWithManager:self.moreManager done:^(HXPhotoModel *model, HXCustomCameraViewController *viewController) {
        UIImage * image;
        if (model.previewPhoto) {
            image = model.previewPhoto;
        } else {
            image = model.thumbPhoto;
        }
        cameraImage(image);
    } cancel:^(HXCustomCameraViewController *viewController) {
        NSSLog(@"取消了");
    }];
}

- (void) openSinglePhotoWithController:(UIViewController *)controller withImage:(void (^)(UIImage *))cameraImage {
    self.singleManager.configuration.saveSystemAblum = YES;
    
    [controller hx_presentAlbumListViewControllerWithManager:self.singleManager done:^(NSArray<HXPhotoModel *> *allList, NSArray<HXPhotoModel *> *photoList, NSArray<HXPhotoModel *> *videoList, BOOL original, HXAlbumListViewController *viewController) {
        HXPhotoModel * model = allList.firstObject;
        UIImage * image;
        if (model.previewPhoto) {
            image = model.previewPhoto;
        } else {
            image = model.thumbPhoto;
        }
        cameraImage(image);
    } cancel:^(HXAlbumListViewController *viewController) {
        NSLog(@"取消了");
    }];

}

- (void) openMorePhotosWithController:(UIViewController *) controller maxNum:(NSInteger) maxNum withImage:(void(^)(NSArray * imageArr, NSArray * array))imageArray; {
    self.moreManager.configuration.maxNum = maxNum;
    self.moreManager.configuration.photoMaxNum = maxNum;
    [controller hx_presentAlbumListViewControllerWithManager:self.moreManager done:^(NSArray<HXPhotoModel *> *allList, NSArray<HXPhotoModel *> *photoList, NSArray<HXPhotoModel *> *videoList, BOOL original, HXAlbumListViewController *viewController) {
        self.listAll = [NSMutableArray arrayWithArray:allList];
        NSMutableArray * array = [NSMutableArray array];
        [array removeAllObjects];
        [self.toolManager writeSelectModelListToTempPathWithList:allList success:^(NSArray<NSURL *> *allURL, NSArray<NSURL *> *photoURL, NSArray<NSURL *> *videoURL) {
            NSSLog(@"所有:%@ - 照片:%@ - 视频:%@",allURL,photoURL,videoURL);
            for (NSURL * str in allURL) {
                NSData *data = [NSData dataWithContentsOfURL:str];
                UIImage *image = [UIImage imageWithData:data];
                [array addObject:image];
                
            }
            
            imageArray(array, allList);
            
        } failed:^{
            
        }];
    } cancel:^(HXAlbumListViewController *viewController) {
        NSLog(@"取消了");
    }];
}
/**
 前往设置开启权限
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }
}

- (void) deleteImageWithIndex:(NSInteger) index {
    if (index >= self.listAll.count) {
        
        return;
    } else {
//        NSMutableArray * allList = [NSMutableArray arrayWithArray:list];
        [self.moreManager afterSelectedListdeletePhotoModel:self.listAll[index]];
        [self.listAll removeObjectAtIndex:index];
    }
}

- (void)dealloc {
    [self.moreManager clearSelectedList];
}

@end
